<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 数据列表 -->
      <el-table :data="orderList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index" label="ID"></el-table-column>
        <!-- 内容 -->
        <el-table-column prop="order_number" label="订单编号"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100px"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100px">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="danger" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100px"></el-table-column>
        <!-- 下单时间 -->
        <el-table-column label="下单时间" width="225px">
          <template #default="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="200px">
          <template #default="">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAddrDialog"
              >编辑
            </el-button>
            <el-button
              type="success"
              icon="el-icon-location-information"
              size="mini"
              @click="showProgressDialog"
              >物流
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 编辑地址的对话框 -->
    <el-dialog
      title="编辑地址"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="addrDialogClose"
    >
      <el-form :model="addrForm" :rules="addrFormRules" ref="addrFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="addr1">
          <el-cascader :options="cityDate" v-model="addrForm.addr1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr2">
          <el-input v-model="addrForm.addr2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="40%">
      <!-- 时间线组件 -->
      <el-timeline>
        <el-timeline-item
          color="#0bbd87"
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 导入省区市县地址信息
import cityDate from '@/assets/js/citydata.js'
// 导入物流进度信息
import progress from '@/assets/js/progress.js'
export default {
  name: 'Order',
  data() {
    return {
      // 请求参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 订单数据列表
      orderList: [],
      // 总数据条数
      total: 0,
      // 编辑对话框的显示与隐藏
      editDialogVisible: false,
      // 地址对象
      addrForm: {
        addr1: [],
        addr2: ''
      },
      // 编辑地址的表单验证规则对象
      addrFormRules: {
        addr1: [{ required: true, message: '请选择省市区县!', trigger: 'blur' }],
        addr2: [{ required: true, message: '请输入详细地址!', trigger: 'blur' }]
      },
      // 渲染省市区/县
      cityDate,
      // 物流进度对话框的显示与隐藏
      progressVisible: false,
      // 物流进度信息
      progressInfo: progress
    }
  },
  created() {
    // 获取订单数据列表
    this.getOrderList()
  },
  methods: {
    // 获取订单数据列表
    async getOrderList() {
      // 发起请求
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      // 失败提示
      if (res.meta.status !== 200) {
        return this.$message.error({ message: '获取订单数据列表失败!', center: true })
      }
      // 成功
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听当前页码改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 监听每页显示条数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 显示地址编辑对话框
    showEditAddrDialog() {
      this.editDialogVisible = true
    },
    // 监听编辑地址对话框的关闭
    addrDialogClose() {
      this.$refs.addrFormRef.resetFields()
    },
    // 显示物流进度对话框
    showProgressDialog() {
      // 由于原项目物流接口的问题,这里使用测试物流信息
      // 显示对话框
      this.progressVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
